export default {
  namespaced: true,
  // 状态清单
  state: () => ({
    token: "",
    userCode: "",
    userName: "",
    userRole: "",
    roleCode: "",
    roleName: "",
    icon: "",
    securityLevel: "",
    fieldSecurityLevel: "",
    // 处理过的menus
    menus: "",
    // 未处理的menus
    startMenus: "",
    menu: "/"
  }),
  // 动作清单
  actions: {
    login({ commit }, params) {
      commit('setToken', params.token)
      commit('setUserCode', params.userCode)
      commit('setUserName', params.userName)
      commit('setUserRole', params.userRole)
      commit('setRoleCode', params.roleCode)
      commit('setRoleName', params.roleName)
      commit('setSecurityLevel', params.securityLevel)
      commit('setFieldSecurityLevel', params.fieldSecurityLevel)
    },
    // 清除缓存
    clear({ commit }) {
      commit('setToken', '')
      commit('setUserCode', '')
      commit('setUserName', '')
      commit('setUserRole', '')
      commit('setIcon', '')
      commit('setRoleCode', '')
      commit('setRoleName', '')
      commit('setSecurityLevel', '')
      commit('setFieldSecurityLevel', '')

      localStorage.removeItem('user.token')
      localStorage.removeItem('user.userCode')
      localStorage.removeItem('user.userName')
      localStorage.removeItem('user.userRole')
      localStorage.removeItem('user.icon')
      localStorage.removeItem('user.roleCode')
      localStorage.removeItem('user.roleName')
      localStorage.removeItem('user.securityLevel')
      localStorage.removeItem('user.fieldSecurityLevel')
    }
  },
  // 设值
  mutations: {
    setToken(state, token) {
      localStorage.setItem("user.token", token)
      state.token = token
    },
    setUserCode(state, userCode) {
      localStorage.setItem("user.userCode", userCode)
      state.userCode = userCode
    },
    setUserName(state, userName) {
      localStorage.setItem("user.userName", userName)
      state.userName = userName
    },
    setUserRole(state, userRole) {
      localStorage.setItem("user.userRole", userRole)
      state.userRole = userRole
    },
    setIcon(state, icon) {
      localStorage.setItem("user.icon", icon)
      state.icon = icon
    },
    setRoleCode(state, roleCode) {
      localStorage.setItem("user.roleCode", roleCode)
      state.roleCode = roleCode
    },
    setRoleName(state, roleName) {
      localStorage.setItem("user.roleName", roleName)
      state.roleName = roleName
    },
    setSecurityLevel(state, securityLevel) {
      localStorage.setItem("user.securityLevel", securityLevel)
      state.securityLevel = securityLevel
    },
    setFieldSecurityLevel(state, fieldSecurityLevel) {
      localStorage.setItem("user.fieldSecurityLevel", fieldSecurityLevel)
      state.fieldSecurityLevel = fieldSecurityLevel
    },
    setMenus: (state, menus) => {
      menus = JSON.stringify(menus)
      localStorage.setItem("user.menus", menus)
      state.menus = menus
    },
    setUserGroupList: (state, userGroupList) => {
      // userGroupList = JSON.stringify(userGroupList)
      localStorage.setItem("user.userGroupList", userGroupList)
      state.userGroupList = userGroupList
    },
    setUserList: (state, userList) => {
      // userList = JSON.stringify(userList)
      localStorage.setItem("user.userList", userList)
      state.userList = userList
    },
    setOnLineuserGroupList: (state, onLineuserGroupList) => {
      // onLineuserGroupList = JSON.stringify(onLineuserGroupList)
      localStorage.setItem("user.onLineuserGroupList", onLineuserGroupList)
      state.onLineuserGroupList = onLineuserGroupList
    },
    setMessageGroupList: (state, messageGroupList) => {
      // messageGroupList = JSON.stringify(messageGroupList)
      localStorage.setItem("user.messageGroupList", messageGroupList)
      state.messageGroupList = messageGroupList
    },
    setResponseMessageList: (state, responseMessageList) => {
      // responseMessageList = JSON.stringify(responseMessageList)
      localStorage.setItem("user.responseMessageList", responseMessageList)
      state.responseMessageList = responseMessageList
    },
    setRequestMessageList: (state, requestMessageList) => {
      // requestMessageList = JSON.stringify(requestMessageList)
      localStorage.setItem("user.requestMessageList", requestMessageList)
      state.requestMessageList = requestMessageList
    }
  },
  // 取值
  getters: {
    // 获取模式
    getToken(state) {
      if (state.token === "") {
        state.token = localStorage.getItem("user.token")
      }
      return state.token
    },
    getUserCode(state) {
      if (state.userCode === "") {
        state.userCode = localStorage.getItem("user.userCode")
      }
      return state.userCode
    },
    getUserName(state) {
      if (state.userName === "") {
        state.userName = localStorage.getItem("user.userName")
      }
      return state.userName
    },
    getUserRole(state) {
      if (state.userRole === "") {
        state.userRole = localStorage.getItem("user.userRole")
      }
      return state.userRole
    },
    getRoleCode(state) {
      if (state.roleCode === "") {
        state.roleCode = localStorage.getItem("user.roleCode")
      }
      return state.roleCode
    },
    getRoleName(state) {
      if (state.roleName === "") {
        state.roleName = localStorage.getItem("user.roleName")
      }
      return state.roleName
    },
    getSecurityLevel(state) {
      if (state.securityLevel === "") {
        state.securityLevel = localStorage.getItem("user.securityLevel")
      }
      return state.securityLevel
    },
    getFieldSecurityLevel(state) {
      if (state.fieldSecurityLevel === "") {
        state.fieldSecurityLevel = localStorage.getItem("user.fieldSecurityLevel")
      }
      return state.fieldSecurityLevel
    },
    getMenus(state) {
      if (state.menus === "") {
        state.menus = localStorage.getItem("user.menus")
      }
      return JSON.parse(state.menus)
    },

    getUserGroupList(state) {
      if (state.userGroupList === "") {
        state.userGroupList = localStorage.getItem("user.userGroupList")
      }
      return JSON.parse(state.userGroupList)
    },
    getUserList(state) {
      if (state.userList === "") {
        state.userList = localStorage.getItem("user.userList")
      }
      return JSON.parse(state.userList)
    },
    getOnLineuserGroupList(state) {
      if (state.onLineuserGroupList === "") {
        state.onLineuserGroupList = localStorage.getItem("user.onLineuserGroupList")
      }
      return JSON.parse(state.onLineuserGroupList)
    },
    getMessageGroupList(state) {
      if (state.messageGroupList === "") {
        state.messageGroupList = localStorage.getItem("user.messageGroupList")
      }
      return JSON.parse(state.messageGroupList)
    },
    getResponseMessageList(state) {
      if (state.responseMessageList === "") {
        state.responseMessageList = localStorage.getItem("user.responseMessageList")
      }
      return JSON.parse(state.responseMessageList)
    },
    getRequestMessageList(state) {
      if (state.requestMessageList === "") {
        state.requestMessageList = localStorage.getItem("user.requestMessageList")
      }
      return JSON.parse(state.requestMessageList)
    }
  }
}
